<?php

namespace CZTools\Hyperf\library;

/**
 * 验证码
 * Class Vcode
 * @package Lib
 */
class Vcode
{

    private $charset = "abcdefghjklmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789";   //随机因子
    private $charset_num = '0123456789';
    private $code;     //验证码文字
    private $codelen = 4;    //验证码显示几个文字
    private $width = 100;   //验证码宽度
    private $height = 40;   //验证码高度
    private $img;       //验证码资源句柄
    private $font;     //指定的字体
    private $fontsize = 20;  //指定的字体大小
    private $fontcolor;     //字体颜色  随机
    private $overdue_sec = 300;//过期时间
    private $token;
    private $storage;
    private $charset_type;  //1 字符串,即 $charset，2纯数字 $charset_num

    //构造类  编写字体
    public function __construct($token, $code = NULL, $charset_type = null)
    {
        $this->font = './4.ttf';
        $this->token = $token;
        $this->storage = cache();

        if ($code) {
            $this->code = $code;
        }

        if ($charset_type) {
            $this->charset_type = $charset_type;
        }
    }

    //创建4个随机码
    public function createCode()
    {
        if ($this->code)
            return $this->code;

        if ($this->charset_type == 2) {
            $this->charset = $this->charset_num;
        }

        $_leng = mb_strlen($this->charset, "utf-8") - 1;
        for ($i = 1; $i <= $this->codelen; $i++) {
            $this->code .= mb_substr($this->charset, mt_rand(0, $_leng), 1, "utf-8");
        }
        $this->storage->set($this->token, $this->code, $this->overdue_sec);
        return $this->code;
    }

    //创建背景
    private function createBg()
    {
        //创建画布 给一个资源jubing
        $this->img = imagecreatetruecolor($this->width, $this->height);

        //背景颜色
        $color = imagecolorallocate($this->img, mt_rand(240, 255), mt_rand(240, 255), mt_rand(240, 255));

        //画出一个矩形
        imagefilledrectangle($this->img, 0, $this->height, $this->width, 0, $color);
    }

    //创建字体
    private function createFont()
    {
        $this->code = strval($this->code);
        $_x = ($this->width / $this->codelen);   //字体长度

        for ($i = 0; $i < $this->codelen; $i++) {
            //文字颜色
            $color = imagecolorallocate($this->img, mt_rand(0, 156), mt_rand(0, 156), mt_rand(0, 156));

            //资源句柄 字体大小 倾斜度 字体长度  字体高度  字体颜色  字体  具体文本
            imagettftext($this->img, $this->fontsize, mt_rand(-30, 30), $_x * $i + mt_rand(1, 5), $this->height / 1.4, $color, $this->font, $this->code[$i]);
        }
    }

    //随机线条
    private function createLine()
    {
        //随机线条
        for ($i = 0; $i < 6; $i++) {
            $color = imagecolorallocate($this->img, mt_rand(0, 156), mt_rand(0, 156), mt_rand(0, 156));
            imageline($this->img, mt_rand(0, $this->width), mt_rand(0, $this->height), mt_rand(0, $this->width), mt_rand(0, $this->height), $color);
        }

        //随机雪花
        for ($i = 0; $i < 45; $i++) {
            $color = imagecolorallocate($this->img, mt_rand(220, 255), mt_rand(220, 255), mt_rand(220, 255));
            imagestring($this->img, mt_rand(1, 5), mt_rand(0, $this->width), mt_rand(0, $this->height), '*', $color);
        }
    }

    //输出背景
    private function outPut()
    {
        //输出图片
        imagepng($this->img);
        $content = ob_get_clean();

        //销毁结果集
        imagedestroy($this->img);
        return response($content, 200, ['Content-Length' => strlen($content)])->contentType('image/png');
    }

    //对外输出
    public function doimg()
    {
        //加载背景
        $this->createBg();

        //加载文件
        $this->createCode();

//        //加载线条
//        $this->createLine();

        //加载字体
        $this->createFont();

        //加载背景
        return $this->outPut();
    }

    //获取验证码
    public function getCode()
    {
        return strtolower($this->code);
    }

    //验证验证码
    public function checkCode($code, $clear = false)
    {
        if (strtolower($this->storage->get($this->token)) == strtolower($code)) {
            if ($clear) $this->clearCode();
            return true;
        }

        if ($clear) $this->clearCode();

        return false;
    }

    //清除验证码
    public function clearCode()
    {
        $this->storage->delete($this->token);
    }

}
